<?php

class Weibo4Php {

	var $client;
    var $friends;
	var $trends;
	var $user;
	var $friendAndHisTrends;
	var $trendPaticipators;
	var $isLogin;

	function __construct( $client, $user) {
		$this->client = $client;
		$this->user = $user;
		$this->friends = $this->getFriends($user->id,1);
		var_dump($this->friends);
		$this->trends = $this->getTrends($user->id,1);
		var_dump($this->trends);
		$this->mapFriendAndTrend();
		$this->isLogin = true;
	}

// ---<friends>-------------------
	function getFriends($uid, $page) {
		if($uid == NULL){
			$uid = $this->user->id;
		}
		$part = $this->client->bilateral($uid, $page);
		if (count($part)==0) {
			return null;
		} elseif (count($part) < 50) {
			return $part;
		} else {
			$page=$page+1;
			$list=getFriends($uid, $page);
			return array_merge($list, $part);
		}		
	}

	// ---<trends>-------------------
	function getTrends($uid, $page) {
		if($uid == NULL){
			$uid = $this->user->id;
		}
		$part = $this->client->get_trends($uid, $page);
		if (count($part)==0) {
			return null;
		} elseif (count($part) < 10) {
			return $part;
		} else {
			$page=$page+1;
			$list=getFriends($uid, $page);
			return array_merge($list, $part);
		}		
	}

	public function mapFriendAndTrend() {
		$this->friendAndHisTrends = array();
		$this->trendPaticipators = array();
		foreach ( $this->friends as $friend) {
			$trends = $this->getTrends($friend->id,1);
			$this->friendAndHisTrends[$friend->id]= $trends;
			
			foreach ($this->trends as $userTrend ) {
				if (array_key_exists($userTrend->hotword, $this->trendPaticipators)) {
					array_push($this->trendPaticipators[$userTrend->hotword], $friend);
				} else {
					$this->trendPaticipators=array($userTrend->hotword=>$friend);
				}
			}
		}
		foreach ($this->trends as $userTrend) {
				if (array_key_exists($userTrend->hotword, $this->trendPaticipators)) {
					array_push($this->trendPaticipators[$userTrend->hotword], $user);
				} else {
					$this->trendPaticipators=array($userTrend->hotword=>$user);
				}
		}
	}
 
	// ---<timeline>-------------------
	function getTimeline($uid, $page=1) {
		$part = $this->client->user_timeline_by_id( $uid , $page, 50 , 0, 0, 1, 1, 0);
		if (count($part)==0) {
			return null;
		} elseif (count($part) < 50) {
			return $part;
		} else {
			$page=$page+1;
			$list=getFriends($uid, $page);
			return array_merge($list, $part);
		}		
	}

	// ---<trendline>-------------------
	function getTrendline($hotword =NULL) {
		if ($hotword==NULL) {
			if (count($this->trends)==0) {
				return null;
			}
		}
		$mark = "#" + $hotword + "#";
		$list = array();
		if (!array_key_exists($userTrend->hotword, $this->trendPaticipators)) {
			return null;
		}
		
		$paticipators = $this->trendPaticipators[$hotword];
		foreach ($paticipators as $paticipator) {
			$uid = $user->id;
			$status = $this->getTimeline($uid);
			foreach ($status as $s) {
				$text = $s->text;
				if ($text==$mark) {
					array_push($list, $s);
				}
			}

		}

		usort($list, "status_sort");
		return $list;
	}

	private function status_sort($a, $b) {
	  if ($a->createdAt == $b->createdAt) return 0;
	  return ($a->createdAt > $b->createdAt) ? -1 : 1;
	}

	// ---<all friends' trends>-------------------
	function getAllFriendTrends() {
		$list = array();
		$empty = array();
		foreach (array_keys($this->friendAndHisTrends) as $key) {
			$trends = $this->friendAndHisTrends[$key];
			if (empty($trends)) {
				$list[$key]=$trends ;
			} else {
				$empty[$key]=$trends ;
			}
		}
		return array_merge($list, $empty);
	}

	function getTrendPaticipator($trend) {
		return $this->trendPaticipators[$trend];
	}

	// ---<actions>-------------------
	function UpdateStatus($statuses) {
		$res = $this->client->update( $status );
		//TODO handle 400 error
	}

	function isTrendFollowed($trend) {
		foreach ($this->trends as $one) {
			if (strcasecmp($one->hotword, $trend) ==0 ) {
				return true;
			}
		}

		return false;
	}

	function trendsFollow($newTrend) {
		if ($this->isTrendFollowed($newTrend)) {
			return "此话题已关注。";
		}
		$userTrend =$this->client->follow_trends($newTrend);
		$userTrend->hotword=$newTrend;
		$userTrend->num="0";
		array_push($this->trends, userTrend);
		
		if (array_key_exists($newTrend, $this->trendPaticipators)) {
			array_unshift($this->trendPaticipators[$newTrend], $this->user);
		} else {
			$this->trendPaticipators=array($newTrend=>$this->user);
		}
	}

	function trendsDestroy($trend_id) {
		$this->client->unfollow_trends($trend_id);
		$i = 0;
		foreach ($this->client->trends as $one) {
			if (strcasecmp($one->trend_id, "".$trend_id)==0) {
				unset($this->trends[$i]);
				//trendPaticipators.remove($one->hotword, user);
				if (array_key_exists($one->hotword, $this->trendPaticipators)) {
					$paticipators=$this->trendPaticipators[$one->hotword];
					$j=0;
					foreach($paticipators as $paticipator){
						if($paticipator->id==$this->user->id){
							unset($paticipator[$j]);
							break;
						}
						$j++;
					}				
				}
				break;
			}
			$i++;
		}
	}

	function getUser() {
		return $this->user;
	}

	function isLogin() {
		return $this->isLogin;
	}

	function shorturl($url) {
		$params = array();
		$params['url_long'] = $url;
		$shorturl=$this->client->oauth->get( 'short_url/shorten', $params);

		return $shorturl->url_short;
	}

}

?>